home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / UTILITY / MIR105.ARJ / MIR_ASC.EXE / lha / WP_GENER.C < prev    next >
Text File  |  1992-04-24  |  6KB  |  207 lines

  1. /*
  2.  * Usage -  wp_gener  < ASCII_file > generic_file
  3.  *
  4.  * WP_GENER Prepares an ASCII file so that it may be used as input for
  5.  *          almost any word processing program.  This program replaces
  6.  *          a hard return with a blank between lines of continuous text.
  7.  *
  8.  * input:   An ASCII text file with lines of 80 bytes or less.
  9.  *
  10.  * output:  Same file with selected line ends replaced by blanks.
  11.  *
  12.  * writeup:  MIR Tutorial TWO, topic ..
  13.  *
  14.  *  Written:    Douglas Lowry   Apr 24 92
  15.  *              Copyright (C) 1992 Marpex Inc.
  16.  *
  17.  *  This program is free software; you may redistribute it and/or
  18.  *  modify it under the terms of the GNU General Public License as
  19.  *  published by the Free Software Foundation; either version 2 of
  20.  *  the License, or (at your option) any later version.
  21.  *
  22.  *  This program is distributed in the hope that it will be useful,
  23.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  24.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  25.  *  GNU General Public License for more details.
  26.  *
  27.  *  You should have received a copy of the GNU General Public
  28.  *  License (file 05LICENS) along with this program; if not,
  29.  *  write to the Free Software Foundation, Inc., 675 Mass Ave,
  30.  *  Cambridge, MA 02139, USA.
  31.  */
  32.  
  33. #include <stdio.h>
  34. #include <stdlib.h>
  35.  
  36. #define     repeat      for(;;)
  37.  
  38. /*
  39.  * declarations 
  40.  */
  41.  
  42. typedef     enum        _bool
  43.              { FALSE = 0, TRUE = 1 }  Bool;
  44.  
  45.     void        Usage_(), process();
  46.     int         classify();
  47.     char        *Cmdname_() {    return( "wp_gener" );  }
  48.  
  49. /*
  50.  * MAIN
  51.  */
  52.  
  53. main( argc, argv )
  54.     int  argc;
  55.     char **argv;
  56. {
  57.     if( argc > 1 )
  58.         Usage_() ;
  59.  
  60.     process( ) ;
  61.  
  62.     exit( 0 );
  63. }
  64. /*
  65.  *  Usage
  66.  */
  67.     void
  68. Usage_()
  69. {
  70.     fprintf( stderr,
  71. "\nUsage:  %s  < ASCII_file > generic_file\n\n\
  72.         Prepares an ASCII file so that it may be used as input for\n\
  73.         almost any word processing program.  This program replaces\n\
  74.         a hard return with a blank between lines of continuous text.\n\n\
  75. input:  An ASCII text file with lines of 80 bytes or less.\n\n",
  76.         Cmdname_() );
  77.     fprintf( stderr,
  78. "output: Same file with selected line ends replaced by blanks.\n\n\
  79. writeup: MIR Tutorial TWO, topic ..\n\n" ) ;
  80.     exit( 1 ) ;
  81. }
  82. #define     EMPTY       0
  83. #define     FIXED       1
  84. #define     LEADER      2
  85. #define     CONTINUOUS  3
  86. #define     BEGIN_FILE  4
  87.  
  88. /*
  89.  *  PROCESS -   Two adjacent lines are continuous if both contain
  90.  *              no tabs or sequences of 3 or more blanks.  One exception
  91.  *              is allowed:  A paragraph leader may have leading white
  92.  *              space.  EMPTY and FIXED lines are fully disqualified.  A
  93.  *              LEADER is part of continuous text only if followed by a
  94.  *              line that is CONTINUOUS.
  95.  */
  96.     void
  97. process( )
  98. {
  99.     unsigned char   buf[ 90 ],
  100.             c ;
  101.     Bool    ctl_z ;     /*  found an end of file marker            */
  102.     int     type,prev,  /*  current and previous lines classified  */
  103.                         /*  as FIXED, LEADER or CONTINUOUS text    */
  104.             len, i ;
  105.  
  106.     type = BEGIN_FILE ;
  107.     ctl_z = FALSE ;
  108.  
  109.     while( fgets( buf, 90, stdin ) != NULL )
  110.     {
  111.         if( ctl_z )
  112.         {
  113.             fprintf( stderr, "FATAL... Text continues after a CTL-Z\n" );
  114.             fprintf( stderr, "\tIs the input really a text file?\n\n" ) ;
  115.             exit( 1 ) ;
  116.         }
  117.         len = strlen( buf ) - 1 ;
  118.         repeat
  119.         {
  120.             c = buf[ len - 1 ] ;
  121.             if( c == '\032' )
  122.             {
  123.                 ctl_z = TRUE ;
  124.                 break ;
  125.             }
  126.             if( c == '\n' || c == '\015' )
  127.                 len-- ;
  128.             else
  129.                 break ;
  130.         }
  131.         if( ctl_z )
  132.             continue ;      /*  Try for more input      */
  133.  
  134.         if( len > 80 )
  135.         {
  136.             fprintf( stderr,"FATAL... line length exceeds 80 bytes.\n");
  137.             Usage_() ;
  138.         }
  139.         buf[ len ] = '\0' ;
  140.  
  141.         prev = type ;
  142.         type = classify( buf, len ) ;
  143.  
  144.         if( type == CONTINUOUS && ( prev == LEADER || prev == CONTINUOUS ))
  145.             putchar( ' ' ) ;
  146.         else if( prev != BEGIN_FILE )
  147.             putchar( '\n' ) ;
  148.  
  149.         for( i = 0 ; i < len ; i++ )
  150.         {
  151.             if( putchar( buf[i] ) != buf[i] )
  152.             {
  153.                 fprintf( stderr, "FATAL... Unable to write.\n\n" ) ;
  154.                 exit( 1 ) ;
  155.             }
  156.         }
  157.     }
  158.  
  159.     putchar( '\n' ) ;
  160.     putchar( '\032' );
  161.     return ;
  162. }
  163. /*
  164.  *  CLASSIFY    Classify line type as EMPTY, FIXED, LEADER or CONTINOUS
  165.  */
  166.     int
  167. classify( buf, len )
  168.     char    buf[90];
  169.     int     len ;
  170. {
  171.     int     txt_bgn,    /*  byte at which text starts   */
  172.             blank_ct,   /*  # of blanks in a row        */
  173.             pt ;
  174.  
  175.     if( !len )
  176.         return( EMPTY ) ;
  177.  
  178.     for( pt = 0 ; pt < len ; pt++ )
  179.     {
  180.         if( !isspace( buf[pt] ))
  181.             break ;
  182.     }
  183.  
  184.     txt_bgn = pt ;
  185.     blank_ct = 0 ;
  186.     for(    ; pt < len ; pt++ )
  187.     {
  188.         if( buf[ pt ] == '\t' )
  189.             return( FIXED ) ;
  190.         if( buf[ pt ] == ' ' )
  191.             blank_ct++ ;
  192.         else if( blank_ct )     /*  any other character     */
  193.         {
  194.             if( blank_ct > 2 )
  195.                 return( FIXED ) ;
  196.             blank_ct = 0 ;
  197.         }
  198.     }
  199.  
  200.     if( blank_ct > 1 )      /*  ...at end of line                  */
  201.         return( FIXED ) ;   /*  else converting an end to blank    */
  202.                             /*  will create a third blank          */
  203.     if( txt_bgn )
  204.         return( LEADER ) ;
  205.  
  206.     return( CONTINUOUS ) ;
  207. }